package sy.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.SysVisitorDaoI;
import sy.model.SysVisitor;

@Repository("sysVisitorDao")
public class SysVisitorDaoImpl implements SysVisitorDaoI {
	
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}
	
	@Override
	public Serializable save(SysVisitor sysVisitor) {
		return getCurrentSession().save(sysVisitor);//直接用
		
	}

	@Override
	public void remove(SysVisitor sysVisitor) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(sysVisitor);
	}

	@Override
	public List<SysVisitor> list() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from SysVisitor order by accessTime desc").list();
	}

	@Override
	public SysVisitor getSysVisitorById(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysVisitor where id = :id";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("id", id);
		List<SysVisitor> list = query.list();
		if (list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<SysVisitor> getSysVisitorByAccount(String accountId) {
		// TODO Auto-generated method stub
		String hql = "from SysVisitor where sysAccount.id = :accountId";
		Query query = getCurrentSession().createQuery(hql);
		query.setString("accountId", accountId);
		List<SysVisitor> list = query.list();
		if (!list.isEmpty()) {
			return list;
		}
		return null;
	}

}
